
package backend.bootstrap;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;
import org.kohsuke.args4j.Option;
import org.kohsuke.args4j.spi.BooleanOptionHandler;
import org.kohsuke.args4j.spi.StringArrayOptionHandler;

import java.util.List;

@ToString
@Getter
@Setter
public class ArgsParam {
    /**
     * Arguments for running Elasticsearch.
     *
     * @param daemonize {@code true} if Elasticsearch should run as a daemon process, or {@code false} otherwise
     * @param quiet {@code false} if Elasticsearch should print log output to the console, {@code true} otherwise
     * @param pidFile absolute path to a file Elasticsearch should write its process id to, or {@code null} if no pid file should be written
     * @param secrets the provided secure settings implementation
     * @param nodeSettings the node settings read from {@code elasticsearch.yml}, the cli and the process environment
     * @param configDir the directory where {@code elasticsearch.yml} and other config exists
     * @param logsDir the directory where log files should be written
     */

    @Option(name = "-d", aliases = {"--daemonize"}, metaVar = "daemonize", usage = "是否后台启动", handler = BooleanOptionHandler.class)
    boolean daemonize;
    @Option(name = "-d", aliases = {"--daemonize"}, metaVar = "daemonize", usage = "是否后台启动", handler = BooleanOptionHandler.class)
    boolean quiet;

    @Option(name = "-p", aliases = {"--pid"}, metaVar = "encrypt/decrypt key", usage = "加解密密钥")
    String pidFile;

    @Option(name = "-c", aliases = {"--config"}, metaVar = "encrypt/decrypt key", usage = "配置")
    String configDir;

    @Option(name = "-l", aliases = {"--logs"}, metaVar = "encrypt/decrypt key", usage = "日志")
    String logsDir;

    @Option(name = "-p", aliases = {"--profile"}, metaVar = "type of string", usage = "配置")
    public String profile = "dev";



    @Option(name = "-v", aliases = {"--version"}, metaVar = "show version", usage = "输出产品版本并退出", handler = BooleanOptionHandler.class)
    public boolean version = false;

    @Option(name = "-h", aliases = {"-?", "--help"}, metaVar = "show help info", usage = "帮助信息", handler = BooleanOptionHandler.class, help = true)
    public boolean help = false;


    @Option(name = "-l", aliases = {"--lists"}, metaVar = "type of lists", usage = "列表元素", handler = StringArrayOptionHandler.class)
    public List<String> lists;

    public static void main(String[] args) throws CmdLineException {
        ArgsParam argsConfig = new ArgsParam();
        CmdLineParser parser = new CmdLineParser(argsConfig);
        parser.setUsageWidth(ArgsConstant.USAGE_WIDTH);
        parser.parseArgument(args);
        // 没有参数
        if (args.length == 0) {
            argsConfig.showHelp(parser);
        }
        // 如果启动参数中包含 帮助参数项， 则打印参数信息
        if (argsConfig.isHelp()) {
            argsConfig.showHelp(parser);
        }
        System.out.println(argsConfig.toString());
    }

    public void showHelp(CmdLineParser parser) {
        System.out.println("参数说明 [options ...] [arguments...]");
        parser.printUsage(System.out);
    }

    public boolean isDaemonize() {
        return daemonize;
    }

    public void setDaemonize(boolean daemonize) {
        this.daemonize = daemonize;
    }

    public boolean isQuiet() {
        return quiet;
    }

    public void setQuiet(boolean quiet) {
        this.quiet = quiet;
    }

    public String getPidFile() {
        return pidFile;
    }

    public void setPidFile(String pidFile) {
        this.pidFile = pidFile;
    }

    public String getConfigDir() {
        return configDir;
    }

    public void setConfigDir(String configDir) {
        this.configDir = configDir;
    }

    public String getLogsDir() {
        return logsDir;
    }

    public void setLogsDir(String logsDir) {
        this.logsDir = logsDir;
    }

    public boolean isVersion() {
        return version;
    }

    public void setVersion(boolean version) {
        this.version = version;
    }

    public boolean isHelp() {
        return help;
    }

    public void setHelp(boolean help) {
        this.help = help;
    }

    public String getProfile() {
        return profile;
    }

    public void setProfile(String profile) {
        this.profile = profile;
    }

    public List<String> getLists() {
        return lists;
    }

    public void setLists(List<String> lists) {
        this.lists = lists;
    }
 
}
